<template>
  <div class="start_body">
    <!-- <div class="start_box">
      <div class="start_tip">星级评定</div>
      <img src='@/assets/common/start.png' 
        class='start_icon' v-for="item in defaultData" 
        data-index="{{item.index}}" 
        bindtap="changeScore" 
        data-type="light"
        wx:key="{{index}}"
        wx:if="{{item.index <= lightScore}}">
      <img src='@/assets/common/start_default.png' 
        class='start_icon' bindtap="changeScore" 
        data-index="1" wx:for="{{defaultData}}" 
        data-index="{{item.index}}" 
        data-type="default"
        wx:key="{{index}}"
        wx:if="{{item.index <= defaultScore}}">
    </div> -->
    <div class="commons_top">
      <div class="commons_back" @click="$router.go(-1)">
        <img src="@/assets/index/back.png" />
      </div>
      <span class="commons_title">添加评论</span>
    </div>
    <div class="start_main">
      <div class="start_box">
        <div class="start_tip">评分</div>
        <el-rate v-model="reviewQuery.score" style="float:left;"></el-rate>
      </div>
      <div class="start_text">
        <textarea placeholder="内容这么赞，快来评论吧~" placeholder-style="color:#C7C7C7;font-size:12px;" maxlength="300" v-model="reviewQuery.content" auto-focus="true"/>
      </div>
      <div class="start_count">{{reviewQuery.content.length}}/300</div>
    </div>
    <div class="submitBtn" @click="submitBtn">提交</div>
  </div>
</template>

<script>
import '@static/css/common.css'
import { Toast } from 'mint-ui';
import { saveReview, replySave } from '@/api/review.js'

export default {
  name:'addreview',
  data() {
    return {
      value:3,
      content:'',
      resourceId: '',
      resourceType: '',
      defaultScore: 0,
      lightScore:5,
      resourceName:'',
      reviewQuery:{
        content :'',
        score: 5,
        resourceType: this.$route.query.resourceType, //资源类型
        resourceId: this.$route.query.resourceId, //资源id ,
        resourceName:this.$route.query.resourceName,// 资源名称
        mainId:0, //所属主评论id：0资源主评论，其他主评论id ,
        fatherId: 0, //(integer, optional): 父级评论id：0资源，其他评论id ,
        // fatherName: '',// (string, optional): 父级评论用户名 ,
      },
    }
  },
  mounted() {
    this.resourceId = this.$route.query.resourceId
    this.resourceType = this.$route.query.resourceType
    if(this.$route.query.resourceName) {
      this.resourceName = this.$route.query.resourceName
    }
  },
  methods: {
    submitBtn() {
      if(this.isLogin()) {
        let str = this.reviewQuery.content.split(" ").join("").length//是否输入空格
        if(this.reviewQuery.score >= 2) {
          if(str != 0) {
            if(this.reviewQuery.content && this.reviewQuery.content.length>=3 && this.userType!=2) {
              saveReview(this.reviewQuery).then(res =>{
                if(res.data.code === 0){
                  Toast('评论成功')
                  this.$router.go(-1)
                  this.reviewQuery.content = ''
                  this.reviewQuery.score = 5
                } else{
                  Toast(res.data.msg)
                }
              })
            } else {
              Toast('请输入3~300个字符的评论内容')
            }
          } else {
            Toast('请输入文字')
          }
        } else {
          Toast('评分不得低于2分')
        }
      }
      // if(this.content){//判断是否提交了content
      //   let params = {
      //     score: this.lightScore, //评分
      //     content: this.content,//评论内容 
      //     resourceId: this.resourceId, // resourceId (string, optional): 资源id ,
      //     resourceType: this.resourceType,// resourceType (integer, optional): 资源类型 
      //     fatherId: 0, // fatherId (integer, optional): 父级评论id：0资源，其他评论id ,
      //     mainId: 0, // mainId (integer, optional): 所属主评论id：0资源主评论，其他主评论id , 
      //     resourceName: this.resourceName
      //   }
      //   if(params.resourceType != 2) {
      //     saveReview(params).then(res => {
      //       if (res.data.code == 0) {
      //         Toast('评论成功');
      //         this.$router.go(-1);
      //       } else {
      //         Toast(res.data.msg);
      //       }
      //     })
      //   } else {
      //     replySave(params).then(res => {
      //       if (res.data.code == 0) {
      //         Toast('评论成功');
      //         this.$router.go(-1);
      //       } else {
      //         Toast(res.data.msg);
      //       }
      //     })
      //   }
      // } else{
      //   Toast('请填写评论');
      // }
    },
  }
}
</script>


<style>
  .start_body textarea::-webkit-input-placeholder {
    font-size: 14px;
  }
  .start_body .el-rate {
    line-height: 20px !important;
  }
</style>
<style scoped>
.start_body{
  background: #F7F7F7;
  height: 100vh;
}
.start_body .commons_top {
  background: #fff;
}
.start_main {
  margin: 0.2rem;
  height: 3.7rem;
  padding: 0.4rem 0.3rem 0.26rem;
  background: #fff;
  border-radius: 0.12rem;
}
.start_box{
  overflow: hidden;
  margin-bottom: 0.3rem;
}
.start_tip{
  float: left;
  margin-right: 0.38rem;
  font-size: 13px;
}
.start_icon{
  width: 0.36rem;
  height: 0.36rem;
  margin-right: 0.3rem;
  float: left;
  margin-top: 0.45rem;
}
.start_text{
  height: 2.7rem;
}
textarea{
  height: 100%;
  width: 100%;
  border:none;
  font-size: 16px;
  outline: none;
  resize: none;
}
.start_count {
  float: right;
  color: #C7C7C7;
  font-size: 12px;
  margin-top: 0.1rem;
}
.submitBtn{
  height: 0.8rem;
  width: 6rem;
  background: linear-gradient(78deg, #FE8666, #FB584E);
  border-radius:0.4rem;
  color: #fff;
  text-align: center;
  line-height: 0.8rem;
  font-size: 15px;
  position: fixed;
  bottom: 0.5rem;
  left: 0.75rem;
}
</style>

